<template>
	<view class="">
		<view class="sign-item" v-if="item.type == 2">
			<view class="sign-item-head">
				<view class="sign-item-head-name">
					《{{ item.contractName }}》
				</view>
				<view class="sign-item-head-right">
					<!-- {{ item.state == 1 ? '生效中' : item.state == 3 ? '已授权' : '已作废' }} -->
					{{ item.state == 1 ? '待签约' : item.state == 2 ? '已签约' : item.state == 3 ? '已解约' : item.state == 4 ? '已拒绝' : item.state == 5 ? '已作废' : ''}}
				</view>
			</view>
			<view class="sign-item-content" v-if="item.isSign == 1">
				<view class="sign-item-content-info">
					收款人：{{ item.payeeName || '' }}
				</view>
				<view class="sign-item-content-info">
					身份证号：{{ item.idCard || '' }}
				</view>
				<view class="sign-item-content-info" v-if="item.state == 1 || item.state == 4">
					申请时间：{{ item.createTime || '' }}
				</view>
				<view class="sign-item-content-info" v-else>
					授权时间：{{ item.createTime || '' }}
				</view>
			</view>
			<view class="sign-item-content" v-if="item.isSign == 0">
				<view class="sign-item-content-info">
					合同编号：{{ item.contractNo }} | {{ item.plateNum }}
				</view>
				<view class="sign-item-content-info">
					合同有效期:{{ item.validPeriod || '' }}
				</view>
			</view>
			<view class="sign-item-footer">
				<!-- <button class="reset-button cancel-btn" @click="onRefuse(item)">拒绝授权</button> -->
				<button class="reset-button confirm-btn" @click="onCancel(item)" v-if="item.state == 2">解除授权</button>
				<button class="reset-button confirm-btn" @click="onRestore(item)" v-if="item.state == 3">恢复授权</button>
				<button class="reset-button confirm-btn" @click="onSign(item)" v-if="item.state == 1 || item.state == 4">去授权</button>
				<button class="reset-button confirm-btn" @click="onDetail(item)" v-if="item.state != 1 && item.contractUrl">查看详情</button>
			</view>
		</view>
		<view class="sign-item" v-if="item.type == 1">
			<view class="sign-item-head">
				<view class="sign-item-head-name">
					《{{ item.contractName }}》
				</view>
				<view class="sign-item-head-right">
					{{ item.state == 1 ? '待签约' : item.state == 2 ? '已签约' : item.state == 3 ? '已解约' : item.state == 4 ? '已拒绝' : item.state == 5 ? '已作废' : ''}}
				</view>
			</view>
			<view class="sign-item-content">
				<view class="sign-item-content-info">
					合同编号：{{ item.contractNo }} | {{ item.plateNum }}
				</view>
				<view class="sign-item-content-info">
					合同有效期:{{ item.validPeriod || '' }}
				</view>
			</view>
			<view class="sign-item-footer">
				<button class="reset-button confirm-btn" @click="onSign(item)" v-if="item.isSign == 0 && item.state == 1">电子合同签约</button>
				<button class="reset-button confirm-btn" @click="onDetail(item)" v-if="item.state != 1">查看详情</button>
			</view>
		</view>
	</view>
</template>

<script>
	import permision from "@/js_sdk/wa-permission/permission.js"
	export default {
		props: {
			item: {
				type: Object,
				default: () => {
					return {}
				}
			}
		},
		data() {
			return {
				
			}
		},
		methods: {
			onDetail(item) {
				if (item.isSign != 0 && item.type == 1) {
					this.signTrueDetail(item)
				} else {
					this.signFalseDetail(item)
				}
			},
			signTrueDetail(item) {
				uni.showLoading({
					title: '合同信息加载中。。。',
					mask: true
				});
				this.wxRequest({
					url: `${this.API1}/seal/driverNotify/getBillNoSignUrl`,
					method: 'POST',
					data: {
						loadingBillNo: item.contractNo
					},
					title: '加载中...',
					success: function(res) {
						var data = res.data.data
						if (res.data.code == 0) {
							//上传、下载
							uni.downloadFile({
								url: data, //下载资源的url
								success: function(res) {
									const filePath = res
									.tempFilePath; //tempFilePath临时文件路径，下载后的文件会存储到一个临时文件
									//新开页面打开文档，支持格式：doc, xls, ppt, pdf, docx, xlsx, pptx。
									uni.openDocument({
										filePath: filePath, //文件路径，可通过 downFile 获得
										showMenu: true, //表示右上角是否有转发按钮
										fileType: 'pdf', //文件类型，指定文件类型打开文件，有效值 doc, xls, ppt, pdf, docx, xlsx, pptx，支付宝小程序仅支持pdf
										success: function(res) {
											uni.hideLoading();
											console.log('打开文档成功');
										}
									});
								}
							});
						}
					}
				})
			},
			// 拒绝
			onRefuse(item) {
				uni.showModal({
					title: '提示',
					content: '是否确认拒绝授权？',
					success: (res) => {
						if (res.confirm) {
							this.wxRequest({
								url: `${this.API1}/seal/driverNotify/getBillNoSignUrl`,
								method: 'POST',
								data: {
									loadingBillNo: item.contractNo
								},
								success: (res) => {
									uni.showToast({
										title: '已拒绝',
										icon: 'none'
									})
									setTimeout(() => {
										this.$emit('reset')
									}, 500)
								},
							})
						}
					}	
				})
			},
			onCancel(item) {
				uni.showModal({
					title: '提示',
					content: '是否确认解除授权？',
					success: (res) => {
						if (res.confirm) {
							this.wxRequest({
								url: `${this.API}/driverYearSign/updateSignStatus?signStatus=3&driverId=${uni.getStorageSync(this.DATA_KEY.userInfo).driverId}&idCard=${item.idCard}`,
								method: 'POST',
								data: {},
								success: (res) => {
									uni.showToast({
										title: '已解约',
										icon: 'none'
									})
									setTimeout(() => {
										this.$emit('reset')
									}, 500)
								},
							})
						}
					}	
				})
			},
			onRestore(item) {
				uni.showModal({
					title: '提示',
					content: '是否确认恢复授权？',
					success: (res) => {
						if (res.confirm) {
							this.wxRequest({
								url: `${this.API}/driverYearSign/updateSignStatus?signStatus=2&driverId=${uni.getStorageSync(this.DATA_KEY.userInfo).driverId}&idCard=${item.idCard}`,
								method: 'POST',
								data: {},
								success: (res) => {
									uni.showToast({
										title: '已恢复',
										icon: 'none'
									})
									setTimeout(() => {
										this.$emit('reset')
									}, 500)
								},
							})
						}
					}	
				})
			},
			signFalseDetail(item) {
				uni.showLoading({
					title: '合同信息加载中。。。',
					mask: true
				});
				//上传、下载
				uni.downloadFile({
					url: item.contractUrl,
					// #ifdef MP-WEIXIN
					filePath: wx.env.USER_DATA_PATH + '/' + item.contractName +
						'.pdf', //指定的本地文件路径   一定要带文件后缀名  不然无法打开   wx.env.USER_DATA_PATH类似本地文件根目录
					// #endif
					success(res) {
						let data = '';
						// #ifdef MP-WEIXIN
						data = res.filePath;
						// #endif
						// #ifdef APP-PLUS
						data = res.tempFilePath;
						// #endif
						uni.openDocument({
							filePath: data,
							showMenu: true, //表示右上角是否有转发按钮
							fileType: 'pdf',
							success: function(res) {
								uni.hideLoading();
								console.log('打开文档成功');
							}
						});
					}
				});
			},
			onSign(item) {
				//#ifdef MP-WEIXIN
				uni.navigateTo({
					url: '/pages/sign/signPreview?isBatch=1' + '&ids=' + item.certificationId
				})
				//#endif
				//#ifdef APP-PLUS
				uni.navigateTo({
					url: '/pages/sign/signPreview?isBatch=1' + '&ids=' + item.certificationId + '&navType=redirectTo&navUrl=/pages/personal/signinglist'
				})
				//#endif
				return
				// if (item.type == 2) {
				// 	uni.navigateTo({
				// 		url: '/pages/sign/signPreview?isBatch=1' + '&ids=' + item.certificationId
				// 	})
				// 	return
				// }
				uni.setStorageSync('loadingBillNo', item.contractNo);
					var that = this
				          var livingBodyState =uni.getStorageSync(that.DATA_KEY.userInfo).livingBodyState
					if(livingBodyState == 1){
						that.wxRequest({
							url: `${this.API1}/seal/driverNotify/authSign`,
							method: 'POST',
							data: {
								mobile: uni.getStorageSync(that.DATA_KEY.userInfo).mobile,
								driverId: uni.getStorageSync(that.DATA_KEY.userInfo).driverId,
								"authType": "1",
								//#ifdef MP-WEIXIN
								redirectUrl:""
								//#endif
								//#ifdef APP-PLUS
								// redirectUrl:"y2t://app/realBack"
								redirectUrl:"https://static-mp-3f064dc3-6a80-42a4-a5e3-0aafc4682c3a.next.bspapp.com/#/"
								//#endif
							},
							title: '加载中...',
							success: function(res) {
								console.log(res.data, '111111111111')
								let url = ""
								//#ifdef MP-WEIXIN
								url = res.data.data
								//#endif
								// #ifdef APP-PLUS
								url = res.data.data
								// #endif
								///seal/driverNotify/authSign  判断用户是否认证  code = 0未认证，1已认证
								if (res.data.code == 0) {
									uni.showModal({
										title: '提示',
										content: '您尚未完成E签宝平台个人身份认证，请完成认证后继续签约',
										confirmText: '去认证',
										success: function(res) {
											if (res.confirm) {
												// #ifdef APP-PLUS
												switch (uni.getSystemInfoSync().platform) {
													case 'android':
														permision.requestAndroidPermission(
															"android.permission.RECORD_AUDIO")
														permision.requestAndroidPermission(
															"android.permission.CAMERA").then((e) => {
														if (e === -1) {
															uni.showToast({
																title: "您已经永久拒绝摄像头权限，请在应用设置中手动开启",
																icon: "none"
															})
														} else if (e === 0) {
															uni.showToast({
																title: "您拒绝了摄像头授权",
																icon: "none"
															})
														} else if (e === 1) {
															
															uni.navigateTo({
																url: '/pages/personal/webview?url=' +encodeURIComponent(url)
															})
														} else {
															uni.showToast({
																title: "授权返回值",
																icon: "none"
															})
														}
													})
														break;
													case 'ios':
														permision.judgeIosPermission("record")
														permision.judgeIosPermission("camera").then((e) => {
														if (e === -1) {
															uni.showToast({
																title: "您已经永久拒绝摄像头权限，请在应用设置中手动开启",
																icon: "none"
															})
														} else if (e === 0) {
															uni.showToast({
																title: "您拒绝了摄像头授权",
																icon: "none"
															})
														} else if (e === 1) {
															uni.navigateTo({
																url: '/pages/personal/webview?url=' +encodeURIComponent(url)
															})
														} else {
															uni.showToast({
																title: "授权返回值",
																icon: "none"
															})
														}
													})
													break;
									}
												// #endif
						
												// #ifdef MP-WEIXIN
												uni.navigateTo({
													url: '/pages/personal/webview?url=' +
														encodeURIComponent(url)
												})
												
												// #endif
											} else if (res.cancel) {
												console.log('用户点击取消');
											}
										}
									});
								} else if (res.data.code == 1) {
									//已实名认证后，调取签署接口生成链接跳转到链接地址签署合同
									that.wxRequest({
										url: `${that.API1}/seal/driverNotify/fileSign`,
										method: 'POST',
										data: {
											mobile: uni.getStorageSync(that.DATA_KEY.userInfo).mobile,
											driverId: uni.getStorageSync(that.DATA_KEY.userInfo).driverId,
											loadingBillNo: item.contractNo,
											loadingOrderId: null,
											"type":"1",
											"authType":"1",
											//#ifdef MP-WEIXIN
											redirectUrl:""
											//#endif
											//#ifdef APP-PLUS
											// redirectUrl:"y2t://app/realBack"
											redirectUrl:"https://static-mp-3f064dc3-6a80-42a4-a5e3-0aafc4682c3a.next.bspapp.com/#/"
											//#endif
										},
										title: '加载中...',
										success: function(res) {
											console.log(res,11111)
											let urls = ""
											//#ifdef MP-WEIXIN
											urls = res.data.data
											//#endif
											//#ifdef APP-PLUS
											urls = res.data.data
											// #endif
											// #ifdef APP-PLUS
											switch (uni.getSystemInfoSync().platform) {
												case 'android':
													permision.requestAndroidPermission(
														"android.permission.RECORD_AUDIO")
													permision.requestAndroidPermission(
														"android.permission.CAMERA").then((e) => {
														if (e === -1) {
															uni.showToast({
																title: "您已经永久拒绝摄像头权限，请在应用设置中手动开启",
																icon: "none"
															})
														} else if (e === 0) {
															uni.showToast({
																title: "您拒绝了摄像头授权",
																icon: "none"
															})
														} else if (e === 1) {
															
															uni.navigateTo({
																url: '/pages/personal/webview?url=' +encodeURIComponent(urls)
															})
														} else {
															uni.showToast({
																title: "授权返回值",
																icon: "none"
															})
														}
													})
													break;
												case 'ios':
													permision.judgeIosPermission("record")
													permision.judgeIosPermission("camera").then((e) => {
														if (e === -1) {
															uni.showToast({
																title: "您已经永久拒绝摄像头权限，请在应用设置中手动开启",
																icon: "none"
															})
														} else if (e === 0) {
															uni.showToast({
																title: "您拒绝了摄像头授权",
																icon: "none"
															})
														} else if (e === 1) {
															uni.navigateTo({
																url: '/pages/personal/webview?url=' +encodeURIComponent(urls)
															})
														} else {
															uni.showToast({
																title: "授权返回值",
																icon: "none"
															})
														}
													})
													break;
											
											}
											
											// #endif
											// #ifdef MP-WEIXIN
											uni.navigateTo({
												url: '/pages/middle/bizpage?url=' +encodeURIComponent(urls)
											})
											// #endiff
						
										}
									})
								}
							},
							fail(err) {
								console.log(err)
							}
						})
					}else{
						uni.showModal({
							title: '提示',
							content: '您尚未完成E签宝平台个人身份认证，请完成认证后继续签约',
							confirmText: '去认证',
							success: function(res) {
								if (res.confirm) {
									let that = this;
									that.wxRequest({
										url: `${that.API}/driverService/getDriverIdCardInfo`,
										method: 'POST',
										data: {
											driverId: that.user.driverId,
											platformCoNo: uni.getStorageSync(that.DATA_KEY.platformCoNo)
										},
										title: '加载中...',
										success: res => {
											let accessToken = res.data.data.accessToken; //认证令牌
											that.accessToken = accessToken
											let taxNumber = res.data.data.taxNumber; //平台税号
											let realName = res.data.data.driverName;
											let idCard = res.data.data.idcard;
											let idPhotoUp = res.data.data.idcard1Base64;
											let idPhotoDown = res.data.data.idcard2Base64;
											that.wxRequestData({
												url: `${that.API2}/api/face/getFaceQRCode?access_token=` + accessToken,
												method: 'POST',
												title: '加载中...',
												data: {
													companyTaxNumber: taxNumber,
													realName: realName,
													idCard: idCard,
													idPhotoUp: idPhotoUp,
													idPhotoDown: idPhotoDown,
													callbackUrl: `${that.API}/driverService/isLivingBodyState`
												},
												success: res => {
													console.log("===================>2",res);
													if (res.data.code == 0) {
														let orderNumber = res.data.data.orderNumber;
														that.orderNumber = orderNumber
														that.wxRequest({
															url: `${that.API}/driverService/saveFaceVerifyOrderNumber`,
															method: 'POST',
															data: {
																driverId: that.user.driverId,
																orderNumber: orderNumber,
																platformCoId: uni.getStorageSync(that.DATA_KEY.platformCoNo) == '0001' ? '100001' : '100002'
															},
															success: res => {
																// #ifdef APP-PLUS
																uni.showModal({
																	title: '提示',
																	content: '是否去进行实名认证?跳转进行实名认证后需要重新打开当前APP!',
																	success: function(res) {
																		if (res.confirm) {
																			console.log('用户点击确定');
																			that.timer = null
																			that.flag = true
																			plus.share.getServices(
																				function(res) {
																					var sweixin = null;
																					console.log(res);
																					for (var i = 0; i < res.length; i++) {
																						var t = res[i];
																						console.log(t);
																						if (t.id == 'weixin') {
																							sweixin = t;
																						}
																					}
																					if (sweixin) {
																						console.log('dddd=', 'a=' + sweixin + '=b');
																						sweixin.launchMiniProgram({
																							// id: 'gh_cec8224b00da',
																							id: 'gh_45eec401ff1c',
																							type: 0, //0-正式版 1.测试版，2，体验版
																							path: 'pages/verify/info/index?id=' + orderNumber
																						});
																					}
																				},
																				function(res) {
																					console.log(JSON.stringify(res));
																				}
																			);
																		} else if (res.cancel) {
																			console.log('用户点击取消');
																			that.timer = null
																		}
																	}
																});
																// #endif
																
																// #ifdef MP-WEIXIN
																uni.showModal({
																	title: '提示',
																	content: '是否去进行实名认证?',
																	success: function(res) {
																		if (res.confirm) {
																			that.timer = null
																			console.log(that.timer)
																			
																			that.flag = true
																			console.log('用户点击确定');
																			uni.navigateToMiniProgram({
																				// appId: 'wxfd2861dc1c86b3a0',
																				appId: 'wxfc1e27e05fdd062b',
																				// path: 'pages/verify/info/index?id=' + orderNumber,
																				path: 'pages/verify/info/index?id=' + orderNumber,
																				envVersion: 'release',
																				success(res) {
																					console.log('跳转成功');
																				},
																				fail(res) {
																					console.log('跳转失败' + JSON.stringify(res));
																				}
																			});
																		} else if (res.cancel) {
																			that.timer = null
																			console.log(that.timer)
																			console.log('用户点击取消');
																		}
																	}
																});
																// #endif
															}
														});
													}
												}
											});
										}
									});
								}else if (res.cancel) {
								console.log('用户点击取消');
								}
					
					},
					// }
				      })
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.reset-button {
		padding: 0;
		margin: 0;
		font-size: inherit;
		background-color: transparent;
		color: inherit;
		position: relative;
		border: 0rpx;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		align-items: center;
		justify-content: center;
		box-sizing: border-box;
		text-align: center;
		text-decoration: none;
		white-space: nowrap;
		vertical-align: baseline;
		transform: translate(0, 0);
	}
	.reset-button.button-hover {
		transform: translate(1px, 1px);
		background: none;
	}
	
	.reset-button::after {
		border: none;
	}
	.sign-item {
		background-color: #fff;
		margin: 20rpx;
		border-radius: 15rpx;
		border-bottom: 3rpx solid #EEEEEE;
		padding:20rpx 10rpx;
		.sign-item-head {
			display: flex;
			align-items: center;
			justify-content: space-between;
			// padding: 30rpx 30rpx 20rpx;
			.sign-item-head-name {
				font-weight: bold;
			}
			.sign-item-head-right {
				font-weight: bold;
			}
		}
		.sign-item-content {
			// font-size: 28rpx;
			// color: #333;
			// padding: 10rpx 30rpx;
			.sign-item-content-info {
				padding-top: 15rpx;
			}
		}
		.sign-item-footer {
			display: flex;
			align-items: center;
			justify-content: flex-end;
			// padding: 0 30rpx 30rpx;
			padding: 16rpx 0;
			button + button {
				margin-left: 16rpx;
			}
			.cancel-btn {
				padding: 0 15rpx;
				border: 2rpx solid #2979ff;
				color: #2979ff;
				height: 50rpx;
				line-height: 50rpx;
				border-radius: 15rpx;
			}
			.confirm-btn {
				padding: 0 15rpx;
				border: 2rpx solid #2979ff;
				color: #2979ff;
				height: 50rpx;
				line-height: 50rpx;
				border-radius: 15rpx;
			}
		}
	}
</style>